#region System
using System;
using System.Collections.Generic;
using System.IO;
using CF.Framework.Engine.Controls;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace CF.Framework.Engine.Effects
{
	public abstract class Effect : IDisposable
	{
		private bool isRunning;
		private bool isDoing;
		private bool isCompleted;

		public event EventHandler EffectStart;
		public event EventHandler EffectStop;
		public event EventHandler EffectBegin;
		public event EventHandler EffectEnd;

		internal Control Target { get; set; }

		public bool IsRunning
		{
			get { return isRunning; }
			protected set
			{
				if (isRunning != value)
				{
					isRunning = value;

					if (value == true)
					{
						FireEvent(EffectStart);
					}
					else
					{
						FireEvent(EffectStop);
					}
				}
			}
		}

		public bool IsDoing
		{
			get { return isDoing; }
			protected set
			{
				if (isDoing != value)
				{
					isDoing = value;

					if (value == true)
					{
						FireEvent(EffectBegin);
					}
					else
					{
						FireEvent(EffectEnd);
					}
				}
			}
		}

		public bool IsCompleted
		{
			get { return isCompleted; }
			set { isCompleted = value; }
		}

		public Effect()
		{
			this.isDoing = false;
			this.IsRunning = false;
		}

		public void Start()
		{
			OnStart();

			IsCompleted = false;
			IsRunning = true;
			IsDoing = true;
		}

		public abstract void OnStart();

		public void Stop()
		{
			IsCompleted = false;
			IsRunning = false;
			IsDoing = false;

			OnStop();
		}

		public abstract void OnStop();

		public void Update(GameTime gameTime)
		{
			if (!this.IsRunning
				|| !this.IsRunning)
			{
				return;
			}

			OnUpdate(gameTime);
		}

		public abstract void OnUpdate(GameTime gameTime);

		public virtual void Dispose()
		{
			this.Target = null;
		}

		private void FireEvent(EventHandler handler)
		{
			if (handler != null)
			{
				handler(this, EventArgs.Empty);
			}
		}
	}
}
